﻿#/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Created on 2011-07-11

日志工厂类

@author: zhongfeng
'''
import logging.config
import os

class LoggerFactory(object):
    _loggerFac = None
    def __init__(self):
        pass
    def __new__(cls):
        '''
                    单态实现，初始化一次 
        '''
        if '_inst' not in vars(cls):
            #curPath = os.path.abspath(os.path.dirname(sys.argv[0]))
            modPath = os.path.dirname(__file__)
            logCfg = os.path.join(modPath,'logging.conf')
            logging.config.fileConfig(logCfg)
            cls._inst = super(LoggerFactory, cls).__new__(cls)
        return cls._inst
        
    @classmethod
    def getLogger(cls,logName='root'):
        if cls._loggerFac == None:
            cls._loggerFac = LoggerFactory()
        if isinstance(logName,type):     #传递的是一个类，去类名
            logName = logName.__name__
        return logging.getLogger(logName)
    
    def __del__(self):
        logging.shutdown()
        
def testMutiThread():
    from threadpool import ThreadPool,WorkRequest
    
    def printlog(msg):
        logger = LoggerFactory.getLogger()
        logger.info('-'.join([msg,ctime()]))
        
    urls = (r'http://www.360buy.com/product/{}.html'.format(str(proid)) for proid in xrange(1,14000))
    #print urls
    #requests = makeRequests(printlog,urls)
    
    print "Creating thread pool with 3 worker threads."
    main = ThreadPool(3)
    
    [main.putRequest(WorkRequest(printlog,[url])) for url in urls ]
    
    main.wait()

if __name__ == '__main__':
    from time import ctime
    for t in range(10):
        logger = LoggerFactory.getLogger('360buy')
        logger.info(' %d this is a test %s' % ( t, ctime() ))   
            
   

    

        